package nick.xbmc.library
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	
	import nick.xbmc.events.AddPathEvent;
	
	public class MovieFactory extends EventDispatcher
	{
		private const ID_XML_LOADED:String = "ID_XML_LOADED";
		
		private var idLoader:URLLoader;
		
		private var server:String = "nickharbin"; 
		//private var server:String = "localhost";
		//private var server:String = "192.168.2.4";
		
		private var prefix:String = "http://" + server + "/xbmcCmds/xbmcHttp?command=queryvideodatabase(";
		private var postfix:String = ")";
		
		private var xml:XML;
		private static var movieXML:XML;
		
		private static var movies:ArrayCollection = new ArrayCollection();
		
		public static var movieLibrary:Array;
		
		public function MovieFactory()
		{
			idLoader = new URLLoader();
			
			idLoader.addEventListener(Event.COMPLETE, idFileLoadedHandler);
			
			this.addEventListener(ID_XML_LOADED, xmlCompletedHandler);
			this.addEventListener(AddPathEvent.COMPLETE, getIDFile);
			
			idLoader.load(new URLRequest(prefix + "select idMovie from movie" + postfix));
		}
		
		private function getIDFile(event:AddPathEvent):void
		{
			for (var i:Number = 0; i < movies.length; i++)
			{
				if (movies[i].IDPath == event.IDPath)
				{
					movies[i].Path = event.Path;
					addImagesToMovieStart(movies[i]);
				}
			}
		}
		
		private function addImagesToMovieStart(movie:Movie):void
		{
			var imageLoader:URLLoader = new URLLoader();
			imageLoader.addEventListener(Event.COMPLETE, addImagesToMovieComplete);
			imageLoader.load(new URLRequest("http://" + server + "/xbmcCmds/xbmcHttp?command=getMovieDetails(" + movie.Path + movie.File + ")"));	
		}
		
		private function addImagesToMovieComplete(event:Event):void
		{
			var info:String = event.target.data;
			
			var title:String = info.substr(info.indexOf("Title:") + 6);
			
			title = title.substr(0, title.indexOf("<li"));
			
			title = title.substring(0, title.lastIndexOf("\n"));
			
			var thumb:String = info.substr(info.indexOf("Thumb:") + 6);
			
			thumb = thumb.substring(0, thumb.indexOf("</html>"));
			
			for (var i:Number = 0; i < movies.length; i++)
			{
				if (movies[i].Title == title)
				{
					movies[i].ThumbInUse = thumb;
					movies[i].FanArtInUse = "special://masterprofile/Thumbnails/Video/Fanart/" + thumb.substring(thumb.lastIndexOf("/") + 1);
				}
			}
		}
		
		private function addPathToMovie(event:Event):void
		{
			var pathXML:XML = new XML(event.target.data);
			var path:String = pathXML.field[0];
			var idPath:String = pathXML.field[1];
			
			dispatchEvent(new AddPathEvent(AddPathEvent.COMPLETE, idPath, path));
		}
		
		protected function addFileToMovie(event:Event):void
		{
			var fileXML:XML = new XML(event.target.data);
			
			for (var i:Number = 0; i < movies.length; i++)
			{
				if (movies[i].IDFile == fileXML.field[0])
				{
					movies[i].File = fileXML.field[1];
					movies[i].IDPath = fileXML.field[2];
					
					var temp:URLLoader = new URLLoader();
					temp.addEventListener(Event.COMPLETE, addPathToMovie);
					temp.load(new URLRequest(prefix + "select strPath,idPath from path where idPath=" + fileXML.field[2] + postfix));
					break;
				}
			}
		}
		
		protected function idFileLoadedHandler(event:Event):void
		{
			xml = new XML(event.target.data);
			
			dispatchEvent(new Event(ID_XML_LOADED));
		}
		
		protected function loadMovie(event:Event):void
		{
			movieXML = new XML(event.target.data);
			
			var tempThumbs:Array;
			
			if (movieXML.field[3].thumb.length() != 0)
			{
				tempThumbs = new Array(movieXML.field[3].thumb.length());
				
				for (var i:Number = 0; i < tempThumbs.length; i++)
				{
					tempThumbs[i] = movieXML.field[3].thumb[i];
				}
			}
			else
			{
				tempThumbs = new Array(movieXML.field[3].thumbs.thumb.length());
				
				for (var x:Number = 0; x < tempThumbs.length; x++)
				{
					tempThumbs[x] = movieXML.field[3].thumbs.thumb[x];
				}
			}
			
			var tempFanart:Array = new Array(movieXML.field[7].fanart.length());
			
			for (var x:Number = 0; x < tempFanart.length; x++)
			{
				tempFanart[x] = movieXML.field[7].fanart.attribute("url") + movieXML.field[7].fanart.thumb[x].text();
			}
			
			var movie:Movie = new Movie();
			movie.Title = movieXML.field[0];
			movie.Plot = movieXML.field[1];
			movie.Tagline = movieXML.field[2];
			movie.ThumbnailURLS = tempThumbs;
			movie.Runtime = movieXML.field[4];
			movie.MPAARating = movieXML.field[5];
			movie.Studio = movieXML.field[6];
			movie.FanArtURLS = tempFanart;
			movie.IDFile = movieXML.field[8];
			movies.addItem(movie);
			
			var temp:URLLoader = new URLLoader();
			temp.addEventListener(Event.COMPLETE, addFileToMovie);
			temp.load(new URLRequest(prefix + "select idFile,strFilename,idPath from files where idFile=" + movie.IDFile + postfix));
		}
		
		public function getMovies():Array
		{
			movieLibrary = movies.toArray();
			movieLibrary.sortOn("Title");
			
			return movieLibrary;
		}
		
		private function xmlCompletedHandler(event:Event):void
		{			
			var idArrayCollection:ArrayCollection = new ArrayCollection();
			
			for (var i:Number = 0; i < xml.field.length(); i++)
			{
				var temp:URLLoader = new URLLoader();
				temp.addEventListener(Event.COMPLETE, loadMovie);
				temp.load(new URLRequest(prefix + "select c00,c01,c03,c08,c11,c12,c18,c20,idFile from movie where idMovie=" + xml.field[i] + postfix));
			}
		}
	}
}